#pragma once

#include <iostream>
#include <fstream>
#include <string>
#include <cstdarg>
#include <ctime>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
using namespace std;
enum
{
    Debug = 0,
    Info,
    Warning,
    Error,
    Fatal,
};
enum
{
    stream = 10,
    OneFile,
    ClassFile,
};
const int defaultstyle = stream;
const string defaultfilename = "log.";
const string defaultmkdir = "log";
string LevelToString(int level)
{
    switch (level)
    {
    case Debug:
        return "Debug";
        break;
    case Info:
        return "Info";
        break;
    case Warning:
        return "Warning";
        break;
    case Error:
        return "Error";
        break;
    case Fatal:
        return "Fatal";
        break;
    defult:
        return "unknow";
        break;
    }
    return nullptr;
}
string getitme()
{
    time_t newtime = time(nullptr);
    char timestr[128];
    struct tm *curr = localtime(&newtime);
    snprintf(timestr, sizeof(timestr), "%d-%d-%d %d:%d:%d", curr->tm_year + 1900, curr->tm_mon + 1, curr->tm_mday, curr->tm_hour, curr->tm_min, curr->tm_sec);
    return timestr;
}
class Log
{
public:
    Log() : _style(defaultstyle), _filename(defaultfilename)
    {
        mkdir(defaultmkdir.c_str(), 0775);
    }
    void writeonefile(const string &levelstr, const string message)
    {
        umask(0);
        int fd = open(levelstr.c_str(), O_CREAT | O_WRONLY | O_APPEND, 0666);
        if (fd < 0)
            return;
        write(fd, message.c_str(), message.size());
        close(fd);
    }
    void writeclaeefile(const string &levelstr, const string message)
    {
        string filerout = defaultmkdir;
        filerout += '/';
        filerout+=defaultfilename;
        filerout += levelstr;
        writeonefile(levelstr,message);
        
        // int fd = open(filerout.c_str(), O_CREAT | O_WRONLY | O_APPEND, 0666);
        // if (fd < 0)
        //     return;
        // write(fd, message.c_str(), message.size());
        // close(fd);
    }
    void writelog(const string &levelstr, const string message)
    {
        switch (_style)
        {
        case stream:
            std::cout << message;
            break;
        case OneFile:
            writeonefile("all", message);
            break;
        case ClassFile:
            writeclaeefile(levelstr, message);
            break;
        default:
            break;
        }
    }
    void massage(int level, const char *format, ...)
    {
        char leftbuffer[1024];
        string levelstr = LevelToString(level);
        string timestr = getitme();
        string idstr = to_string(getpid());
        snprintf(leftbuffer, sizeof(leftbuffer), "[%s][%s][%s]", levelstr.c_str(), timestr.c_str(), idstr.c_str());
        va_list args;
        va_start(args, format);
        char rightbuffer[1024];
        vsnprintf(rightbuffer, sizeof(rightbuffer), format, args);
        va_end(args);
        string loginfo = leftbuffer;
        loginfo += rightbuffer;
        writelog(levelstr, loginfo);
    }

private:
    int _style;
    string _filename;
};
Log lg;